package main.car;

import lombok.extern.slf4j.Slf4j;
import main.model.ElaModel;
import main.model.ElaRepository;
import org.apache.tomcat.util.json.JSONParser;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

//@Configuration
@Slf4j
public class Sae {
    @Resource
    private ElaRepository repository;

    @Value("http://csae.sae-china.org")
    private String baseUrl;

    @Resource
    private WebDriver driver;

//    @Scheduled(fixedDelay = 20000000)
    public void main() {
        repository.deleteAll();
        String url = "";
        Set<String> urls = new HashSet<>();
        url = baseUrl + "/c522";
        driver.get(url);
        addUrl(driver.getPageSource(), urls, driver, url);
        url = baseUrl + "/c523";
        driver.get(url);
        addUrl(driver.getPageSource(), urls, driver, url);
//        urls.forEach(System.out::println);
        saveInfo(driver,urls);
    }

    private void addUrl(String html, Set<String> urls, WebDriver driver, String url) {
        Document document = Jsoup.parse(html);
        Element element = document.selectFirst("body > div.m_auto > div.ls_right.fr.mt_20 > div > a:nth-child(5)");
        String pageInfo = element.attr("href");
        Integer page = Integer.parseInt(pageInfo.substring(pageInfo.lastIndexOf("=") + 1));
        System.out.println(page);
        Elements elements;
        for (int i = 1; i < page + 1; i++) {
            System.out.println(url + "?page=" + i);
            if (i != 1) {
                driver.get(url + "?page=" + i);
                document = Jsoup.parse(driver.getPageSource());
            }
            elements = document.select("div.ls_right.fr.mt_20 > ul > li> a");
            for (Element e : elements) {
                urls.add(baseUrl + "/" + e.attr("href"));
            }
        }
    }

    private void saveInfo(WebDriver driver,Set<String> url){
        log.info("startAll==>{}",url.size());
        Document document;
        Element element;
        Elements elements;
        ElaModel elaModel;
        int count = 1;
        for (String s : url) {
            elaModel = new ElaModel();
            log.info("start....{} ==>{}",s,count);
            count++;
            driver.get(s);
            document = Jsoup.parse(driver.getPageSource());
            //标题
            element = document.selectFirst("body > div.m_auto > div.ls_right.fr.mt_20 > h4");
            System.out.println(element.text());
            elaModel.setTitle(element.text());
            elaModel.setId(element.text().hashCode()^(element.text().hashCode()>>16));

            //时间来源
            element = document.selectFirst("body > div.m_auto > div.ls_right.fr.mt_20 > h5");
            String[] timeAndSource = element.text().split(" ");
            elaModel.setTime(timeAndSource[0]);
            elaModel.setSource(timeAndSource[1]);
            System.out.println(element.text());
            //内容
            element = document.selectFirst("body > div.m_auto > div.ls_right.fr.mt_20 > div.contentdiv.f_14.clr_3");
            elaModel.setContent(element.html());

            elaModel.setCreateTime(new Date());
            elaModel.setType(2);
            repository.save(elaModel);
            log.info("end......");
        }
    }

}
